const app = getApp()
let qCloud = require("../../index.js");

Page({
    /** 页面的初始数据 */
    data: {
        imgPath: qCloud.config.STATIC_IMAGE_PATH,
        operateMark: "2", // 操作标志:1-新增;2-修改;3-生产;4-完成;
        produceTaskId: null, // 生产任务ID:2-修改、3-生产、4-完成时必送
        produceTaskCode: "", // 生产任务编号==款号
        produceTaskBedNo: null, // 生产任务床号
        produceTaskName: "", // 生产任务名称=="款号-床号"
        produceTotal: null, // 生产数量
        unitPrice: null, // 单价
        totalPrice: null, // 总价
        startTime: "", // 开始时间
        deliveryTime: "", // 交货时间
        wagesModel: 1, // 工资模式
        wagesMonth: "", // 工资月份
        switchDay: 1, // 月切日期
        switchDayTitle: "1日", // 月切日期标题
        switchDayList: [], // 月切日期的选择集合
        oldWagesMonth: null, // 工资月份(查询到的，重复选择工资模式时，用来显示用)
        oldSwitchDay: null, // 月切日期(查询到的，重复选择工资模式时，用来显示用)
        oldSwitchDayTitle: null, // 月切日期标题(查询到的，重复选择工资模式时，用来显示用)
        memberList: [], // 负责人列表-
        memberId: "", // 负责人ID
        memberName: "", // 负责人名称
        departmentList: [], // 部门列表-
        departmentId: "", // 部门ID
        departmentName: "", // 部门名称
        showSpecsTypeModal: false, // 尺码类型标记
        specsTypeTemplateList: [], // 尺码类型模板列表-
        specsTypeTemplate: {}, // 尺码类型模板-
        specsTypeList: [], // 尺码类型列表-
        specsType: "", // 尺码类型
        specsTypeTitle: "", // 尺码类型
        showProcessModalStatus: false, // 工序模板对话框标记
        processTemplateList: [], // 工序模板列表-
        processTemplateName: "",
        processList: [], // 工序列表-
        technologicalRequirements: "", // 工艺要求
        imageId: "", // 图片
        imageUrl: "",
        showImg: false, // 是否显示图片上传组件
        showModalStatus: false
    },

    // 款号
    produceTaskCodeInput(e) {
        let self = this
        self.setData({
            produceTaskCode: e.detail.value.trim()
        });

        // 设置任务名称=="款号-床号"
        self.setProduceTaskName();
    },

    // 床号
    produceTaskBedNoInput(e) {
        let self = this
        self.setData({
            produceTaskBedNo: e.detail.value.trim()
        });

        // 设置任务名称=="款号-床号"
        self.setProduceTaskName();
    },

    // 任务名称
    produceTaskNameInput(e) {
        let self = this
        self.setData({
            produceTaskName: e.detail.value.trim()
        });
    },

    // 设置任务名称=="款号-床号"
    setProduceTaskName() {
        let self = this;
        let produceTaskCode = self.data.produceTaskCode;
        let produceTaskBedNo = self.data.produceTaskBedNo;

        let produceTaskName = "";
        if (produceTaskCode && produceTaskBedNo) {
            produceTaskName = produceTaskCode + "-" + produceTaskBedNo;
        }

        self.setData({
            produceTaskName: produceTaskName
        });
    },

    // 生产数量
    produceTotalInput(e) {
        let self = this;
        let produceTotal = e.detail.value;
        // 初始化生产数量
        self.initProduceTotal(produceTotal);
    },

    // 初始化生产数量
    initProduceTotal(produceTotal) {
        let self = this;
        self.setData({
            produceTotal: produceTotal
        });

        // 计算总价
        self.countTotalPrice();
        // 初始化工序数量
        self.initProcessTotal();
    },

    // 初始化工序数量
    initProcessTotal() {
        let self = this;
        for (let i = 0; i < self.data.processList.length; i++) {
            if (self.data.processList[i]["completedProcess"] > 0 || self.data.processList[i]["approvalProcess"] > 0) {
                continue;
            }
            self.data.processList[i]["processTotal"] = self.data.produceTotal;
        }
        self.setData({
            processList: self.data.processList
        });
    },

    // 单价
    unitPriceInput(e) {
        let self = this;
        let unitPrice = e.detail.value;

        self.setData({
            unitPrice: Number(unitPrice)
        });

        // 计算总价
        self.countTotalPrice();
    },

    // 计算总价
    countTotalPrice() {
        let self = this;
        self.setData({
            totalPrice: self.data.produceTotal * self.data.unitPrice
        });
    },

    // 开始时间
    bindStartTimeChange(e) {
        let self = this;
        self.setData({
            startTime: e.detail.value
        })
    },

    // 交货时间
    bindDeliveryTimeChange(e) {
        let self = this;
        self.setData({
            deliveryTime: e.detail.value
        });
    },

    // 工资模式
    bindWagesModelChange(e) {
        let self = this
        self.setData({
            wagesModel: e.detail.value,
            wagesMonth: self.data.oldWagesMonth,
            switchDay: self.data.oldSwitchDay,
            switchDayTitle: self.data.oldSwitchDayTitle,
        });
    },

    // 工资月份
    bindWagesMonthChange(e) {
        let self = this
        self.setData({
            wagesMonth: e.detail.value,
            switchDay: self.data.oldSwitchDay,
            switchDayTitle: self.data.oldSwitchDayTitle,
        })
    },

    // 月切日期
    bindSwitchDayChange(e) {
        let self = this
        let switchDayInfo = self.data.switchDayList[e.detail.value];
        self.setData({
            wagesMonth: self.data.oldWagesMonth,
            switchDay: switchDayInfo.code,
            switchDayTitle: switchDayInfo.title,
        })
    },

    // 负责人
    bindMemberChange(e) {
        let self = this;
        let index = e.detail.value;
        let memberInfo = self.data.memberList[index];
        self.setData({
            memberId: memberInfo.memberId,
            memberName: memberInfo.memberName
        });
    },

    // 指定部门
    chooseDept: function () {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });

        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
            showModalStatus: true
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export()
            });
        }.bind(this), 200);
    },

    // 指定部门的弹出框
    hideShowModal: function (e) {
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                showModalStatus: false
            })
        }.bind(this), 200);
    },

    // 搜索指定部门
    searchInput: function (e) {
        let self = this;
        self.getDeptListAll(e.detail.value)
    },

    // 选择指定部门
    confirmChoose: function (e) {
        let index = e.currentTarget.dataset.index;
        let departmentInfo = this.data.departmentList[index];
        this.setData({
            departmentId: departmentInfo.departmentId,
            departmentName: departmentInfo.departmentName,
            showModalStatus: false
        });
    },

    // 部门
    bindDepartmentChange(e) {
        let self = this;
        let index = e.detail.value;
        let departmentInfo = self.data.departmentList[index];
        self.setData({
            departmentId: departmentInfo.departmentId,
            departmentName: departmentInfo.departmentName
        });
    },

    // 清空部门
    clearDepartment(e) {
        let self = this;
        self.setData({
            departmentId: "", // 部门ID
            departmentName: "", // 部门名称,
            showModalStatus: false
        });
    },

    // 选择尺码类型模板
    bindSizeTypeChange(e) {
        let self = this;
        let index = e.detail.value;
        let specsTypeTemplate = self.data.specsTypeTemplateList[index];

        let specsList = [];
        let templateSpecsList = specsTypeTemplate.specsList;
        for (let i = 0; i < templateSpecsList.length; i++) {
            let specsInfo = {
                specsId: null,
                specs: templateSpecsList[i],
                quantity: 0,
                deletedMark: false,
            };
            specsList.push(specsInfo);
        }
        let specsTypeList = [{
            colourId: null,
            colour: "",
            specsList: specsList,
            deletedMark: false,
        }];

        self.setData({
            showSpecsTypeModal: true,
            specsTypeTemplate: specsTypeTemplate,
            specsTypeList: specsTypeList,
            specsType: specsTypeTemplate.code,
            specsTypeTitle: specsTypeTemplate.title
        });
    },

    // 颜色
    colourInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let specsTypeList = self.data.specsTypeList;
        specsTypeList[index]["colour"] = e.detail.value.trim();
        self.setData({
            specsTypeList: specsTypeList
        });
    },

    // 添加尺码类型
    addSpecsType(e) {
        let self = this;

        let specsList = [];
        let templateSpecsList = self.data.specsTypeTemplate.specsList;
        for (let i = 0; i < templateSpecsList.length; i++) {
            let specsInfo = {
                specsId: null,
                specs: templateSpecsList[i],
                quantity: 0,
                deletedMark: false,
            };
            specsList.push(specsInfo);
        }
        let specsType = {
            colourId: null,
            colour: "",
            specsList: specsList,
            deletedMark: false,
        };
        let specsTypeList = self.data.specsTypeList;
        specsTypeList.push(specsType);

        self.setData({
            specsTypeList: specsTypeList
        });
    },

    // 删除尺码类型
    delSpecsType(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let specsTypeList = self.data.specsTypeList;
        specsTypeList[index]["deletedMark"] = true;
        self.setData({
            specsTypeList: specsTypeList
        });

        // 计算尺码数量
        self.countSpecsTotal();
    },

    // 尺码
    specsInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let stIndex = e.currentTarget.dataset.stindex;
        let specsTypeList = self.data.specsTypeList;
        specsTypeList[index].specsList[stIndex]["specs"] = e.detail.value.trim();
        self.setData({
            specsTypeList: specsTypeList
        });
    },

    // 尺码数量
    specsTotalInput(e) {
        let self = this;
        let quantity = e.detail.value;

        let index = e.currentTarget.dataset.index;
        let stIndex = e.currentTarget.dataset.stindex;
        let specsTypeList = self.data.specsTypeList;
        specsTypeList[index].specsList[stIndex]["quantity"] = quantity;
        self.setData({
            specsTypeList: specsTypeList
        });

        // 计算尺码数量
        self.countSpecsTotal();
    },

    // 添加尺码
    addSpecs(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let specsTypeList = self.data.specsTypeList;
        let specsInfo = {
            specsId: null,
            specs: "",
            quantity: 0,
            deletedMark: false,
        };
        specsTypeList[index].specsList.push(specsInfo);

        self.setData({
            specsTypeList: specsTypeList
        });
    },

    // 删除尺码
    delSpecs(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let stIndex = e.currentTarget.dataset.stindex;
        let specsTypeList = self.data.specsTypeList;
        specsTypeList[index].specsList[stIndex]["deletedMark"] = true;

        self.setData({
            specsTypeList: specsTypeList
        });

        if (specsTypeList[index].specsList[stIndex]["quantity"] != "") {
            // 计算尺码数量
            self.countSpecsTotal();
        }
    },

    // 计算尺码数量
    countSpecsTotal() {
        let self = this;

        let produceTotal = 0;
        for (let i = 0; i < self.data.specsTypeList.length; i++) {
            for (let j = 0; j < self.data.specsTypeList[i].specsList.length; j++) {
                if (!self.data.specsTypeList[i]["deletedMark"] && !self.data.specsTypeList[i].specsList[j]["deletedMark"]) {
                    produceTotal = produceTotal + Number(self.data.specsTypeList[i].specsList[j]["quantity"]);
                }
            }
        }

        // 初始化生产数量
        self.initProduceTotal(produceTotal);
    },

    // 添加工序
    addProcess(e) {
        let self = this
        let processList = self.data.processList;
        let process = {
            produceTaskProcessId: null,
            processName: "",
            processTotal: self.data.produceTotal,
            unitPrice: 0,
            deletedMark: false,
        };
        processList.push(process);
        self.setData({
            processList: processList
        });
    },

    // 删除工序
    delProcess(e) {
        let self = this
        let index = e.currentTarget.dataset.index;
        let processList = self.data.processList;

        if (self.data.processList[index]["completedProcess"] > 0 || self.data.processList[index]["approvalProcess"] > 0) {
            wx.showToast({
                title: "该工序已经存在上数记录，不允许删除",
                icon: "error"
            });
            return;
        }

        processList[index]["deletedMark"] = true;
        self.setData({
            processList: processList
        });
    },

    // 工序名称
    processNameInput(e) {
        let self = this;
        self.data.processList[e.currentTarget.dataset.index]["processName"] = e.detail.value.trim();
        self.setData({
            processList: self.data.processList
        });
    },

    // 工序数量
    processTotalInput(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let processTotal = e.detail.value;

        if (self.data.processList[index]["completedProcess"] > 0 || self.data.processList[index]["approvalProcess"] > 0) {
            wx.showToast({
                title: "该工序已经存在上数记录，不允许修改数量",
                icon: "error"
            });
            return;
        }

        self.data.processList[e.currentTarget.dataset.index]["processTotal"] = processTotal;
        self.setData({
            processList: self.data.processList
        });
    },

    // 工序单价
    processUnitPriceInput(e) {
        let self = this;
        let unitPrice = e.detail.value;

        self.data.processList[e.currentTarget.dataset.index]["unitPrice"] = unitPrice;
        self.setData({
            processList: self.data.processList
        });
    },

    // 导入工序模板
    importProcessTemplate(e) {
        let self = this;
        let index = e.currentTarget.dataset.index;
        let processTemplate = self.data.processTemplateList[index];
        let processList = [];
        let subProcessTemplateList = processTemplate["subProcessTemplateList"];
        for (let i = 0; i < subProcessTemplateList.length; i++) {
            processList.push(subProcessTemplateList[i]);
        }
        self.setData({
            processList: processList,
            processTemplateList: self.data.processTemplateList,
            showProcessModalStatus: false
        });

        // 初始化工序数量
        self.initProcessTotal();
    },

    // 显示对话框(工序模板)
    showProcessModal: function () {
        // 显示遮罩层
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
            showProcessModalStatus: true
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export()
            });
        }.bind(this), 200);
    },

    // 隐藏对话框(工序模板)
    hideProcessModal: function () {
        // 隐藏遮罩层
        let animation = wx.createAnimation({
            duration: 200,
            timingFunction: "linear",
            delay: 0
        });
        this.animation = animation;
        animation.translateY(300).step();
        this.setData({
            animationData: animation.export(),
        });
        setTimeout(function () {
            animation.translateY(0).step();
            this.setData({
                animationData: animation.export(),
                showProcessModalStatus: false
            })
        }.bind(this), 200);
    },

    // 工艺图片
    chooseImage() {
        let self = this;
        wx.chooseMedia({
            count: 1,
            mediaType: ["image"],
            sourceType: ["album", "camera"],
            maxDuration: 30,
            camera: "back",
            success(res) {
                wx.uploadFile({
                    url: qCloud.api.uploadFile, //仅为示例，非真实的接口地址
                    filePath: res.tempFiles[0].tempFilePath,
                    name: "file",
                    formData: {
                        "file": res.tempFiles[0].tempFilePath,
                        "fileType": "6"
                    },
                    header: qCloud.buildSessionHeader(),
                    success(response) {
                        let resJson = JSON.parse(response.data);
                        if (resJson.code == 200) {
                            self.setData({
                                imageId: resJson.data.filesId,
                                imageUrl: resJson.data.filesUrl,
                                showImg: true
                            });
                        }
                    },

                    fail(error) {
                        wx.showToast({
                            title: error,
                            icon: "error"
                        })
                    }
                })
            },
            fail(e) {

            }
        });
    },

    // 工艺要求
    technologicalRequirementsInput(e) {
        let self = this;
        self.setData({
            technologicalRequirements: e.detail.value.trim()
        });
    },

    // 工序模板名称
    processTemplateNameInput(e) {
        let self = this;
        self.setData({
            processTemplateName: e.detail.value.trim()
        });
    },

    // 提交工序模板
    saveTemplate(e) {
        let self = this;
        let processList = self.data.processList; // 工序列表

        let subProcessList = [];
        for (let i = 0; i < processList.length; i++) {
            let processInfo = processList[i];
            if (processInfo.customName != "") {
                let subProcessTemplateInfo = {
                    processName: processInfo.processName, // 工序名称
                    processPrice: processInfo.unitPrice, // 工序单价
                    deletedMark: false,
                };
                subProcessList.push(subProcessTemplateInfo);
            }
        }

        if (subProcessList.length > 0) {
            let params = {
                processInfo: {
                    processName: self.data.processTemplateName,
                    deletedMark: false,
                    subProcessList: subProcessList,
                }
            };
            app.fetchApis(self, qCloud.api.common_submitProcess, params, "POST", function (e) {
                let result = e.data;
                if (result.code == 200) {
                    wx.showToast({
                        title: "保存工序模板成功！",
                        icon: "success"
                    });
                    self.queryProcessList();
                }
            });
        } else {
            wx.showToast({
                title: "没有有效的工序，无法保存为工序模板。",
                icon: "error"
            });
        }

        self.closePopupBox();
    },

    // 显示弹出框
    showPopupBox() {
        let self = this;
        self.setData({
            processTemplateName: "",
            popupBoxMark: true
        });
    },

    // 关闭弹出框
    closePopupBox() {
        let self = this;
        self.setData({
            processTemplateName: "",
            popupBoxMark: false
        });
    },

    /** 生命周期函数--监听页面加载 */
    onLoad: function (options) {
        let self = this;
        self.setData({
            showProcessModalStatus: false,
            produceTaskId: options.produceTaskId,
        });

        // 查询生产任务详情
        self.queryProduceTaskDetails();

        // 初始化尺码类型模板列表
        self.initSpecsTypeTemplateList();

        // 获取部门列表
        self.getDeptListAll("");

        // 获取当前公司用户信息
        self.getMyOrganUserList();

        // 查询工序列表
        self.queryProcessList();
    },

    /** 查询生产任务详情 */
    queryProduceTaskDetails(e) {
        let self = this;
        let params = {
            produceTaskId: self.data.produceTaskId
        }
        app.fetchApis(self, qCloud.api.producetask_queryProduceTaskDetails, params, "POST", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;

                self.setData({
                    produceTaskCode: data.produceTaskCode, // 生产任务编号==款号
                    produceTaskBedNo: data.produceTaskBedNo, // 生产任务床号
                    produceTaskName: data.produceTaskName, // 生产任务名称=="款号-床号"
                    produceTotal: data.produceTotal, // 生产数量
                    unitPrice: data.unitPrice, // 单价
                    totalPrice: data.totalPrice, // 总价
                    startTime: data.startTime, // 开始时间
                    deliveryTime: data.deliveryTime, // 交货时间
                    wagesModel: data.wagesModel, // 工资模式
                    wagesMonth: data.wagesMonth, // 工资月份
                    switchDay: data.switchDay, // 月切日期
                    switchDayTitle: data.switchDay + "日", // 月切日期标题
                    oldWagesMonth: data.wagesMonth, // 工资月份
                    oldSwitchDay: data.switchDay, // 月切日期
                    oldSwitchDayTitle: data.switchDay + "日", // 月切日期标题
                    departmentId: data.departmentId, // 部门ID
                    departmentName: data.departmentName, // 部门名称
                    specsType: data.specsType, // 尺码类型
                    technologicalRequirements: data.technologicalRequirements, // 工艺要求
                    imageId: data.imageId, // 图片
                    qrImageId: data.qrImageId
                });
                // 获取图片信息
                self.getFilesUrl(data.imageId);

                // 转换尺码类型列表
                self.convertSpecsTypeList(data.produceTaskSpecsList);
                // 转换尺码类型
                self.convertSpecsTypeTitle(data.specsType);
                // 转换尺码类型模板
                self.convertSpecsTypeTemplate(data.specsType);
                // 转换工序列表
                self.convertProcessList(data.produceTaskProcessList);
            }
        });
    },

    // 获取图片信息
    getFilesUrl: function (imageId) {
        let self = this;
        let params = {
            filesId: imageId
        }
        app.fetchApis(self, qCloud.api.getFileUrl, params, "GET", function (e) {
            let result = e.data;
            if (result.code == 200) {
                let data = result.data;
                self.setData({
                    imageUrl: data.filesUrl,
                    showImg: true
                });
            }
        });
    },

    // 转换尺码类型列表
    convertSpecsTypeList(produceTaskSpecsList) {
        let self = this;

        let specsTypeList = [];
        let colourIndex = [];
        for (let i = 0; i < produceTaskSpecsList.length; i++) {
            let produceTaskSpecs = produceTaskSpecsList[i];

            if (colourIndex.length > 0) {
                let ci = -1;
                for (let j = 0; j < colourIndex.length; j++) {
                    if (produceTaskSpecs.colourId == colourIndex[j]) {
                        ci = j;
                        break;
                    }
                }

                if (ci == -1) {
                    colourIndex.push(produceTaskSpecs.colourId);

                    let specsList = [{
                        specsId: produceTaskSpecs.produceTaskSpecsId,
                        specs: produceTaskSpecs.specs,
                        quantity: produceTaskSpecs.specsTotal,
                        deletedMark: produceTaskSpecs.deletedMark,
                    }];
                    let specsTypeInfo = {
                        colourId: produceTaskSpecs.colourId,
                        colour: produceTaskSpecs.colour,
                        specsList: specsList,
                        deletedMark: false,
                    };
                    specsTypeList.push(specsTypeInfo);
                } else {
                    let specsInfo = {
                        specsId: produceTaskSpecs.produceTaskSpecsId,
                        specs: produceTaskSpecs.specs,
                        quantity: produceTaskSpecs.specsTotal,
                        deletedMark: produceTaskSpecs.deletedMark,
                    };
                    specsTypeList[ci]["specsList"].push(specsInfo);
                }
            } else {
                colourIndex.push(produceTaskSpecs.colourId);

                let specsList = [{
                    specsId: produceTaskSpecs.produceTaskSpecsId,
                    specs: produceTaskSpecs.specs,
                    quantity: produceTaskSpecs.specsTotal,
                    deletedMark: produceTaskSpecs.deletedMark,
                }];
                let specsTypeInfo = {
                    colourId: produceTaskSpecs.colourId,
                    colour: produceTaskSpecs.colour,
                    specsList: specsList,
                    deletedMark: false,
                };
                specsTypeList.push(specsTypeInfo);
            }
        }

        self.setData({
            showSpecsTypeModal: true,
            specsTypeList: specsTypeList // 尺码类型列表
        });
    },

    // 转换尺码类型
    convertSpecsTypeTitle(specsType) {
        let self = this;

        let specsTypeTitle = "";
        if (specsType == "SML") {
            specsTypeTitle = "SML尺码";
        } else if (specsType == "NS") {
            specsTypeTitle = "数字尺码";
        } else if (specsType == "CS") {
            specsTypeTitle = "自定义尺码";
        }
        self.setData({
            specsTypeTitle: specsTypeTitle
        });
    },

    // 转换尺码类型模板
    convertSpecsTypeTemplate(specsType) {
        let self = this;

        let specsTypeTemplate = {};
        if (specsType == "SML") {
            specsTypeTemplate = self.data.specsTypeTemplateList[0];
        } else if (specsType == "NS") {
            specsTypeTemplate = self.data.specsTypeTemplateList[1];
        } else if (specsType == "CS") {
            specsTypeTemplate = self.data.specsTypeTemplateList[2];
        }
        self.setData({
            specsTypeTemplate: specsTypeTemplate,
        });
    },

    // 转换工序列表
    convertProcessList(produceTaskProcessList) {
        let self = this;

        let processList = [];
        for (let i = 0; i < produceTaskProcessList.length; i++) {
            let produceTaskProcess = produceTaskProcessList[i];
            let processInfo = {
                produceTaskProcessId: produceTaskProcess.produceTaskProcessId,
                processName: produceTaskProcess.processName,
                processTotal: produceTaskProcess.processTotal,
                unitPrice: produceTaskProcess.unitPrice,
                deletedMark: produceTaskProcess.deletedMark,
            };
            processList.push(processInfo);
        }

        self.setData({
            processList: processList // 工序列表
        });
    },

    /** 初始化尺码类型模板列表 */
    initSpecsTypeTemplateList() {
        let self = this
        let specsTypeTemplateList = [{
                title: "SML尺码",
                code: "SML",
                specsList: ["S", "M", "L", "XL", "2XL", "3XL", "4XL"]
            },
            {
                title: "数字尺码",
                code: "NS",
                specsList: ["25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35"]
            },
            {
                title: "自定义尺码",
                code: "CS",
                specsList: []
            }
        ];
        let switchDayList = [{
            'title': '1日',
            'code': 1
        }, {
            'title': '2日',
            'code': 2
        }, {
            'title': '3日',
            'code': 3
        }, {
            'title': '4日',
            'code': 4
        }, {
            'title': '5日',
            'code': 5
        }, {
            'title': '6日',
            'code': 6
        }, {
            'title': '7日',
            'code': 7
        }, {
            'title': '8日',
            'code': 8
        }, {
            'title': '9日',
            'code': 9
        }, {
            'title': '10日',
            'code': 10
        }, {
            'title': '11日',
            'code': 11
        }, {
            'title': '12日',
            'code': 12
        }, {
            'title': '13日',
            'code': 13
        }, {
            'title': '14日',
            'code': 14
        }, {
            'title': '15日',
            'code': 15
        }, {
            'title': '16日',
            'code': 16
        }, {
            'title': '17日',
            'code': 17
        }, {
            'title': '18日',
            'code': 18
        }, {
            'title': '19日',
            'code': 19
        }, {
            'title': '20日',
            'code': 20
        }, {
            'title': '21日',
            'code': 21
        }, {
            'title': '22日',
            'code': 22
        }, {
            'title': '23日',
            'code': 23
        }, {
            'title': '24日',
            'code': 24
        }, {
            'title': '25日',
            'code': 25
        }, {
            'title': '26日',
            'code': 26
        }, {
            'title': '27日',
            'code': 27
        }, {
            'title': '28日',
            'code': 28
        }];
        self.setData({
            switchDayList: switchDayList,
            specsTypeTemplateList: specsTypeTemplateList
        });
    },

    /** 获取部门列表 */
    getDeptListAll: function (value) {
        let self = this;

        let params = {
            deptId: qCloud.session.getUser().organId,
            deptName: value
        };

        app.fetchApis(self, qCloud.api.getDeptListAll, params, "GET", function (e) {
            let result = e.data;

            if (result.code == 200) {
                let data = result.data;

                let departmentList = [];
                for (let j = 0; j < data.length; j++) {
                    let departmentInfo = data[j];
                    if (departmentInfo.isDefault != 'Y') {
                        let department = {
                            departmentId: departmentInfo.deptId,
                            departmentName: departmentInfo.deptNameStr
                        };
                        departmentList.push(department);
                    }
                }

                self.setData({
                    departmentList: departmentList
                });
            }
        });
    },

    /** 获取当前公司用户信息 */
    getMyOrganUserList: function () {
        let self = this;

        let params = {
            deptId: qCloud.session.getUser().organId,
        };

        app.fetchApis(self, qCloud.api.getMyOrganUserList, params, "GET", function (e) {
            let result = e.data;

            if (result.code == 200) {
                let data = result.data;

                let memberList = [];
                for (let j = 0; j < data.length; j++) {
                    let staffInfo = data[j];
                    let staff = {
                        memberId: staffInfo.userId,
                        memberName: staffInfo.realName
                    };
                    memberList.push(staff);
                }

                self.setData({
                    memberList: memberList
                });
            }
        });
    },

    /** 查询工序列表 */
    queryProcessList: function () {
        let self = this;
        let params = {};
        app.fetchApis(self, qCloud.api.common_queryProcessList, params, "POST", function (e) {
            let result = e.data;

            if (result.code == 200) {
                let data = result.data;

                let processTemplateList = [];
                for (let i = 0; i < data.processList.length; i++) {
                    let processInfo = data.processList[i];

                    let subProcessTemplateList = [];
                    for (let j = 0; j < processInfo.subProcessList.length; j++) {
                        let subProcessInfo = processInfo.subProcessList[j];
                        let subProcessTemplate = {
                            processId: subProcessInfo.processId,
                            processName: subProcessInfo.processName,
                            processTotal: 1,
                            unitPrice: subProcessInfo.processPrice,
                            deletedMark: false,
                        };
                        subProcessTemplateList.push(subProcessTemplate);
                    }

                    let processTemplate = {
                        processId: processInfo.processId,
                        processName: processInfo.processName,
                        subProcessTemplateList: subProcessTemplateList
                    };
                    processTemplateList.push(processTemplate);
                }

                self.setData({
                    processTemplateList: processTemplateList
                });
            }
        });
    },

    /** 提交生产任务 */
    submitProduceTask() {
        let self = this;

        let title = self.checkParams();
        if (title != "") {
            wx.showToast({
                title: title,
                icon: "error"
            });
            return;
        }

        let produceTaskSpecsList = []; // 生产任务规格列表
        let specsTypeList = self.data.specsTypeList;
        for (let i = 0; i < specsTypeList.length; i++) {
            let specsTypeInfo = specsTypeList[i];
            if (specsTypeList.length > 1 && specsTypeInfo.colour == "") {
                wx.showToast({
                    title: "请输入颜色",
                    icon: "error"
                });
                return;
            }
            for (let j = 0; j < specsTypeInfo.specsList.length; j++) {
                let specsInfo = specsTypeInfo.specsList[j];
                if (specsInfo.specs != "") {
                    let produceTaskSpecs = {
                        produceTaskSpecsId: specsInfo.specsId, // 生产任务规格ID:2-修改时必送
                        produceTaskSpecsCode: null, // 生产任务规格编号
                        specsType: self.data.specsType, // 尺码类型
                        colourId: specsTypeInfo.colourId, // 颜色
                        colour: specsTypeInfo.colour, // 颜色
                        specs: specsInfo.specs, // 尺码
                        quantity: specsInfo.quantity, // 规格总量
                        deletedMark: specsInfo.deletedMark,
                        unitPrice: 0, // 单价
                        totalPrice: 0, // 总价
                        serialNumber: 0 // 序号
                    };
                    produceTaskSpecsList.push(produceTaskSpecs);
                }
            }
        }

        let produceTaskProcessList = []; // 生产任务工序列表
        let processList = self.data.processList;
        for (let i = 0; i < processList.length; i++) {
            let processInfo = processList[i];
            if (processInfo.produceTaskProcessId == "" && processInfo.processName == "") {
                continue;
            }
            let produceTaskProcess = {
                produceTaskProcessId: processInfo.produceTaskProcessId, // 生产任务工序ID:2-修改时必送
                produceTaskProcessCode: null, // 生产任务工序编号
                processId: processInfo.processId, // 工序ID
                processName: processInfo.processName, // 工序名称
                processTotal: processInfo.processTotal, // 工序总量
                unitPrice: processInfo.unitPrice, // 单价
                deletedMark: processInfo.deletedMark,
                serialNumber: 0 // 序号
            };
            produceTaskProcessList.push(produceTaskProcess);
        }
        // 生产任务工序列表为空
        if (produceTaskProcessList.length == 0) {
            wx.showToast({
                title: "请选择工序信息",
                icon: "error"
            });
            return;
        }

        let params = {
            operateMark: self.data.operateMark, // 操作标志:1-新增;2-修改;3-生产;4-完成;
            produceTaskId: self.data.produceTaskId, // 生产任务ID:2-修改、3-生产、4-完成时必送
            produceTaskCode: self.data.produceTaskCode, // 生产任务编号==款号
            produceTaskBedNo: self.data.produceTaskBedNo, // 生产任务床号
            produceTaskName: self.data.produceTaskName, // 生产任务名称=="款号-床号"
            produceTotal: self.data.produceTotal, // 生产数量
            unitPrice: self.data.unitPrice, // 单价
            totalPrice: self.data.totalPrice, // 总价
            startTime: self.data.startTime, // 开始时间
            deliveryTime: self.data.deliveryTime, // 交货时间
            wagesModel: self.data.wagesModel, // 工资模式
            wagesMonth: self.data.wagesMonth, // 工资月份
            switchDay: self.data.switchDay, // 月切日期
            departmentId: self.data.departmentId, // 部门ID
            departmentName: self.data.departmentName, // 部门名称
            specsType: self.data.specsType, // 尺码类型
            technologicalRequirements: self.data.technologicalRequirements, // 工艺要求
            imageId: self.data.imageId, // 图片
            produceTaskSpecsList: produceTaskSpecsList, // 生产任务规格列表,
            produceTaskProcessList: produceTaskProcessList, // 生产任务工序列表
            colourList: specsTypeList, // 尺寸颜色信息集合
        }

        app.fetchApis(self, qCloud.api.producetask_submitProduceTask, params, "POST", function (e) {
            let result = e.data;

            if (result.code == 200) {
                wx.navigateTo({
                    url: "/pages/produce/index",
                });
            }
        });
    },

    /** 校验参数 */
    checkParams() {
        let self = this;
        if (self.data.produceTaskCode == "") {
            return "请输入款号";
        }
        if (self.data.produceTaskBedNo == "") {
            return "请输入床号";
        }
        if (self.data.produceTotal == "" || self.data.produceTotal == 0) {
            return "请输入数量";
        }
        if (self.data.startTime == "") {
            return "请选择开始时间";
        }
        if (self.data.deliveryTime == "") {
            return "请选择交货时间";
        }
        return "";
    },

    /** 生命周期函数--监听页面初次渲染完成 */
    onReady: function () {},
    /** 生命周期函数--监听页面显示 */
    onShow: function () {},
    /** 生命周期函数--监听页面隐藏 */
    onHide: function () {},
    /** 生命周期函数--监听页面卸载 */
    onUnload: function () {},
    /** 页面相关事件处理函数--监听用户下拉动作 */
    onPullDownRefresh: function () {},
    /** 页面上拉触底事件的处理函数 */
    onReachBottom: function () {},
    /** 用户点击右上角分享 */
    onShareAppMessage: function () {}
});
